package com.company.cloud.behavior.modules.app.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.behavior.modules.api.response.UserScoreItemModel;
import com.company.cloud.behavior.modules.app.controller.req.UserScoreSaveParam;
import com.company.cloud.behavior.modules.app.service.UserScoreService;
import com.company.cloud.behavior.modules.base.entity.UserCommentInfo;
import com.company.cloud.behavior.modules.base.entity.UserScoreInfo;
import com.company.cloud.behavior.modules.base.service.UserCommentInfoService;
import com.company.cloud.behavior.modules.base.service.UserScoreInfoService;
import com.company.cloud.behavior.modules.cache.service.BehaviorCacheService;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.common.redis.service.RedisUtils;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.common.utils.StrSplitUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Service
public class UserScoreServiceImpl implements UserScoreService {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private UserScoreInfoService userScoreInfoService;

    @Autowired
    private BehaviorCacheService behaviorCacheService;

    @Autowired
    private UserCommentInfoService userCommentInfoService;

    @Override
    public Integer saveUserScore(UserScoreSaveParam params, LoginSimpleUserInfo userInfo) {
        Integer count = userScoreInfoService.count(new QueryWrapper<UserScoreInfo>()
                .eq("company_no", userInfo.getCompanyNo())
                .eq("user_id", userInfo.getId())
                .eq("obj_type", params.getObjType())
                .eq("obj_no", params.getObjNo())
                .eq("rows_state", BaseConstant.EnabledEnum.ENABLED.getCode())
        );
        if (count > 0) {
            return null;
        }
        // 添加评分
        UserScoreInfo entity=new UserScoreInfo();
        entity.setCompanyNo(userInfo.getCompanyNo());
        entity.setUserId(userInfo.getId());
        entity.setObjType(params.getObjType());
        entity.setObjNo(params.getObjNo());
        entity.setObjIsUpdown(BaseConstant.YNEnum.YES.getCode());
        entity.setItemScore(JSON.toJSONString(params.getItemScore()));
        Integer totleScore = 0;
        for (UserScoreItemModel userScoreItem : params.getItemScore()) {
            totleScore = totleScore + userScoreItem.getScore();
        }
        entity.setScore(new BigDecimal(totleScore).divide(BigDecimal.valueOf(params.getItemScore().size()), 1, BigDecimal.ROUND_HALF_UP));
        entity.addCreateParam(userInfo);
        entity.addEditParam(userInfo);
        userScoreInfoService.save(entity);
        behaviorCacheService.updateScoreCount(params.getObjType(), params.getObjNo(), 1);

        // 添加评论
        if (CheckUtils.hasText(params.getContent()) || !CollectionUtils.isEmpty(params.getImage())) {
            UserCommentInfo commentEntity=new UserCommentInfo();
            commentEntity.setCompanyNo(userInfo.getCompanyNo());
            commentEntity.setUserId(userInfo.getId());
            commentEntity.setContent(params.getContent());
            commentEntity.setImage(StrSplitUtils.list2string(params.getImage()));
            commentEntity.setCommentTime(LocalDateTime.now());
            commentEntity.setObjType(params.getObjType());
            commentEntity.setObjNo(params.getObjNo());
            commentEntity.setParentId(0);
            commentEntity.setMainId(0);
            commentEntity.setTreePath(","+entity.getId().toString()+",");
            commentEntity.addCreateParam(userInfo);
            commentEntity.addEditParam(userInfo);
            userCommentInfoService.save(commentEntity);
            behaviorCacheService.updateCommentCount(entity.getObjType(), entity.getObjNo(), 1);
        }
        return entity.getId();
    }

}
